package cn.jiyun.service.impl;

import cn.jiyun.common.R;
import cn.jiyun.dao.EmployeeDao;
import cn.jiyun.exption.NameExption;
import cn.jiyun.pojo.Employee;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

@Service
@Transactional
public class EmployeeService implements cn.jiyun.service.EmployeeService {
   @Resource
   private EmployeeDao employeeDao;

    @Override
    public R login(Employee employee) {
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeDao.selectOne(qw);
        if (one==null){
            return  R.error("用户名不存在");
        }
        //判断密码  加密md5
        if (!DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()).equals(one.getPassword())){
            return  R.error("密码错误");
        }
        if (one.getStatus()!=1){
            return R.error("账户被冻结");
        }
        return R.success(one);
    }

    @Override
    public R page(Page page, QueryWrapper<Employee> qw) {
        Page<Employee> page1 = employeeDao.selectPage(page, qw);
        if (page1==null){
            R.error("没有数据");
        }
        return R.success(page);
    }

    @Override
    public Employee hx(int id) {
        return employeeDao.selectById(id);
    }

    @Override
    public R update(Employee employee) {
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeDao.selectOne(qw);
        String s=DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        employee.setPassword(s);
        employee.setUpdateTime(LocalDateTime.now());
//        输入的名称 123   查询出来的名称 查询出来的不为空   数据库有123
//        one !=null 数据库不为空 代表有该名称
//
        System.out.println(employee.getUsername());
        if (one != null  ){

                if(employee.getUsername().equals(one.getUsername())){
                    int c=employeeDao.updateById(employee);
                    if (c>0){
                        return R.success("修改成功");
                    }
                    return R.error("修改失败");

                }
                    throw new NameExption(employee.getUsername());







        }else {
            int c=employeeDao.updateById(employee);
            if (c>0){
                return R.success("修改成功");
            }
            return R.error("修改失败");
        }

//        Date sj=new Date();
//        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format1 = format.format(sj);
//        employee.setUpdateTime(format1);

    }

    @Override
    public R zt(Employee employee) {
//        employee.setStatus(0);
        int i = employeeDao.updateById(employee);
        return R.success("修改成功");
    }

    @Override
    public R add(Employee employee) {
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeDao.selectOne(qw);
        if (one!=null){
            throw new NameExption(employee.getUsername());
        }
        String s=DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        employee.setPassword(s);
//        Date sj=new Date();
//        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format1 = format.format(sj);
//        employee.setCreateTime(format1);
//        employee.setUpdateTime(format1);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        int c=employeeDao.insert(employee);
        if (c>0){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }


}
